var http = require('http');
var url = require('url');
var fs=require('fs');


function serverStaticFile(res,path,contentType,responseCode) {
    if(!responseCode) responseCode=200;
    fs.readFile(__dirname + path,function (err, data) {
        if(err){
            res.writeHead(500,{'Content-Type':'text/plain'});
            res.end('500 -Internal Error');
        }else{
            res.writeHead(responseCode,{'Content-Type':contentType});
            res.end(data);

        }
    })

}

var server = http.createServer(function (req, res) {
    // var path=url.parse(req.url.path);
    // var pathname = url.parse(req.url).pathname;
    var pathname = req.url.replace(/\/?(?:\?.*)?$/,'').toLowerCase();
    console.log(req);
    switch (pathname) {
        case "":
            serverStaticFile(res,'/public/home.html','text/html');
            break;
        case "/about" :
            serverStaticFile(res,'/public/about.html','text/html');
            break;
        case "/img/logo.png" :
            serverStaticFile(res,'/public/img/logo.png','image/png');
            break;
        default :
            serverStaticFile(res,'/public/404.html','text/html');
            break;
    }


}).listen('3000');